import i18n from 'i18n-js'
import { IProduct, ISchedulerConfig, DataType, ISchedulerOption, getProductLanguageByConfig } from '@/config/product'
import languageModel from '@/models/language'
import { getProductConfig, ISchedule } from './tool'


// 布尔值默认配置
const defaultBoolenOptions = [
  { value: true, label: 'OPEN' },
  { value: false, label: 'CLOSE' }
]


// 处理定时配置
function handleSchedulerConfigItem(item: ISchedulerConfig) {
  if (!item.attr) {
    return undefined
  }
  switch (item.dataType) {
    case DataType.Bool:
      // 布尔值没有配置选项的话，就用默认的
      if (!item.options) {
        item.options = JSON.parse(JSON.stringify(defaultBoolenOptions))
      }
      return item;
    case DataType.Enum:
      // 枚举值，没有配置选项，也直接返回不合法
      if (!item.options) {
        return undefined
      }
      return item;
    case DataType.Number:
      return item;
    default:
      break;
  }
  return undefined;
}

export function getSchedulerConfig(config = getProductConfig()): ISchedulerConfig[] {
  console.log('getSchedulerConfig', config.schedulerConfig)
  if (config.schedulerConfig && config.schedulerConfig.length > 0) {
    console.log('config.schedulerConfig.map(i => handleSchedulerConfigItem(i)!)', config.schedulerConfig.map(i => handleSchedulerConfigItem(i)!))
    const arr: ISchedulerConfig[] = config.schedulerConfig.map(i => handleSchedulerConfigItem(i)!).filter(i => !!i)
    return arr
  }
  return []
}

export function getSchedulerOptionName({ config = getProductConfig(), option }: { config?: IProduct, option: ISchedulerOption }) {
  return getProductLanguageByConfig(config, languageModel.key, option.label) ?? i18n.t(option.label)
}

export function getSchedulerOptionNameByValue({ config = getProductConfig(), scheduleConfig, value }:
  { config?: IProduct, scheduleConfig: ISchedulerConfig, value: any }) {
  const sConfig = handleSchedulerConfigItem(scheduleConfig)
  const option = sConfig!.options?.find((i) => i.value === value)
  if (option) {
    return getSchedulerOptionName({ config, option })
  }
  return ''
}

export function getSchedulerNumberTextByValue({ config = getProductConfig(), scheduleConfig, value }:
  { config?: IProduct, scheduleConfig: ISchedulerConfig, value: any }) {
  return value !== undefined ? `${value}${getSchedulerUnit({ config, scheduleConfig })}` : ''
}

export function getSchedulerName({ config = getProductConfig(), scheduleConfig }: { config?: IProduct, scheduleConfig: ISchedulerConfig }) {
  return getProductLanguageByConfig(config, languageModel.key, scheduleConfig.name) || scheduleConfig.name
}

export function getSchedulerUnit({ config = getProductConfig(), scheduleConfig }: { config?: IProduct, scheduleConfig: ISchedulerConfig }) {
  return getProductLanguageByConfig(config, languageModel.key, scheduleConfig.unit) ?? ''
}

export function getSchedulerFunctionText({ schedule, isFocus = false }: { schedule: ISchedule, isFocus?: boolean }) {
  if (!schedule.functionText || isFocus) {
    if (schedule.attrs) {
      const config = getProductConfig()
      const attrsKeys = Object.keys(schedule.attrs)
      let textArr: string[] = []
      attrsKeys.forEach((key) => {
        const scheduleConfig = config.schedulerConfig.find(s => s.attr === key)
        if (scheduleConfig) {
          const value = schedule.attrs![key]
          if (scheduleConfig.dataType === DataType.Number) {
            const valueText = getSchedulerNumberTextByValue({ scheduleConfig, value })
            textArr.push(`${getSchedulerName({ scheduleConfig })}:${valueText}`)
          } else {
            textArr.push(getSchedulerOptionNameByValue({ scheduleConfig, value, }))
          }
        }
      })
      schedule.functionText = textArr.join('、')
    }
    console.log('getSchedulerFunctionText', isFocus, schedule.functionText)
  }
  return schedule.functionText
}
